/**
 * 部门js
 */
var userChannel = {
	curPage : 0,
	pageSize : 10,
	dataCache : null,

	startEvent : function() {
		// 按回车键登录事件
		document.onkeydown = function(e) {
			var ev = document.all ? window.event : e;
			if (ev.keyCode == 13) {
				userChannel.curPage = 1;
				userChannel.setPageChange(null);
				userChannel.search();
			}
		};
	},

	bindEvent : function() {
		// 查询
		$('#searchData').click(function() {
			userChannel.curPage = 1;
			userChannel.setPageChange(null);
			userChannel.search();
		});

		$('#addData').on('click', function() {
			var path = APP_ADDRESS + 'mgr/sysMgr/userChannel/userChannelAdd.jsp';
			var returnStatus = false;
			var dialog = art.dialog.open(path, {
				title : '添加渠道',
				width : 800,
				height : 400,
				ok : function() {
					returnStatus = this.iframe.contentWindow.submitForm();
					if (returnStatus) {
						userChannel.setPageChange(null);
						userChannel.search();

					}
					return false;
				},
				cancel : true
			});

			if (returnStatus) {
				dialog.close();
			}

		});
	},

	// 设置分页
	setPageChange : function(delCurPage) {
		var url = 'mgr/userChannelCtrl/list';
		if (typeof (pageIdx) != "undefined") {
			userChannel.curPage = pageIdx;
		}

		var id = $.trim($('#searchModForm input[name="id"]').val());
		 
		var name = $.trim($('#searchModForm input[name="name"]').val());
		var params = {
			'curPage' : userChannel.curPage,
			'pageSize' : userChannel.pageSize,
		};

		var cb = function(data) {
			if (data.resultCode == 200) {
				var totalCount = data.size;
				var pageTotal = (totalCount / userChannel.pageSize > 0) ? Math
						.ceil(totalCount / userChannel.pageSize) : 1;
				pageHelper.init(pageTotal, totalCount, userChannel.search,
						delCurPage); // 翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},

	// 查询交易记录
	search : function(pageIdx) {
		var url = '/mgr/userChannelCtrl/list';
		if (typeof (pageIdx) != "undefined") {
			userChannel.curPage = pageIdx;
		}

		var id = $.trim($('#searchModForm input[name="id"]').val());
		 
		var name = $.trim($('#searchModForm input[name="name"]').val());
		var params = {
			'curPage' : userChannel.curPage,
			'pageSize' : userChannel.pageSize,
			'id' : id,
			'name' : name
		};
		var cb = function(data) {
			$('#tableList tbody').empty();
			var tr = '';

			if (data.result != null && data.result.length > 0) {
				for (var i = 0; i < data.result.length; i++) {

					var singleObj = data.result[i];

					//更改日期格式

					var createdOn = getSmpFormatDateByLong(
							singleObj.cts, true);
					tr += '<tr><td>'
							+ (i + 1)
							+ '</td><td><input type="hidden" name="id" value="'
							+ singleObj.id
							+ '"/>'
							+ singleObj.channelId
							+ '</td><td>'
							+ singleObj.name
							+ '</td><td>'
							+ singleObj.channelDesc
							+ '</td><td>'
							+ createdOn
							//	+ '</td><td>'
							//+ '<a type="button" name="userChannel_96" class="btn btn-success btn-xs updateData" style="margin-left: 5px;display:none;">'
							//+ '<i class="fa fa-edit"></i>修改</a>'
							+ '</td></tr>';
				}
			} else {
				tr = '<tr><td colspan="6" style="text-align: center;color: red;">暂无数据</td></tr>';
			}
			$('#tableList tbody').html(tr);
			userChannel.bindEventOtherBtn();
			jsUtils.controlFunctionBtn();
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEventOtherBtn : function() {

		$('.updateData').unbind();
		$('.updateData').on(
				'click',
				function() {
					var thisObj = $(this);
					var id = thisObj.parent().parent().find('input[name="id"]')
							.eq(0).val();
					
					var name = thisObj.parent().parent().find('input[name="name"]')
					.eq(0).val();
					
					var companyDes = thisObj.parent().parent().find('input[name="companyDes"]')
					.eq(0).val();
					
					var companyType = thisObj.parent().parent().find('input[name="companyType"]')
					.eq(0).val();
					
					
					 
				 
					art.dialog.data('id', id);
					art.dialog.data('name', name);
					art.dialog.data('companyDes', companyDes);
					art.dialog.data('companyType', companyType);
					var path = APP_ADDRESS + 'mgr/sysMgr/userChannel/userChannelUpdate.jsp';
					art.dialog.open(path, {
						title : '渠道信息修改',
						width : 800,
						height : 400,
						ok : function() {
							var returnStatus = this.iframe.contentWindow
									.submitForm();
							if (returnStatus) {
								userChannel.setPageChange(userChannel.curPage);
								userChannel.search(userChannel.curPage);
							}
							return false;
						},
						cancel : true
					});
				});
	},

	init : function() {
		userChannel.setPageChange(null);
		userChannel.search();
		userChannel.startEvent();
		userChannel.bindEvent();
	}
};

$(document).ready(function() {
	userChannel.init();
});

//提供给外部刷新调用
function refashData() {
	userChannel.setPageChange(null);
	userChannel.search();
}
